<?php
/**
 * @author wyl
 * @date 2015-11-10 13:36:43
 */
Yii::import('application.components.autobid.*');
Yii::import('application.components.cronjob.class.*');
Yii::import('dao.bidAuction.*');
Yii::import('application.components.Log');
Yii::import('application.components.auctionCache.*');
Yii::import('application.components.auctionCache.customerCacheAuctionBidInfo');
class AutobidworkerCommand extends CConsoleCommand{
	public $auctionId;
	public $_worker;
	public	$id;
	public $_log;
	public $customers;
	
	public function init(){
		$this->_log=new Log();
	}
	
	/**
	 * 判断是否执行
	 */
	public function validate($auctionId){
	    $rand=rand(1, 100);
	    $auctionPool=new AuctionPool();
	    $auctionPool= $auctionPool->findByAttributes([
	        "auction_id"=>$auctionId
	    ]);
	    $result=false;
	    $interval=$auctionPool->end_time-time();
	   
	   if(10 < $interval && $interval<=15){
			if (1 <=$rand  && $rand < 2){
                $result=true;
            }
	    }elseif(5 < $interval  && $interval<=10){
			if (1 < $rand && $rand<= 10){
                $result=true;
            }
	    }elseif(3 < $interval && $interval<=5){
			 if (1 < $rand  and $rand< 20){
                $result=true;
            }
	    }elseif(0 < $interval && $interval<=3){
	           $result=true;
	    }
	    return $result;
	}
	
	public function run($args){
		$this->id=$args[0];
		$this->workAcquisition();
	}
	
	//get gearman instance lazy require
	public function worker(){
		if (!$this->_worker instanceof GearmanWorker) {
			$this->_worker = new GearmanWorker();
			$this->_worker->addServer('127.0.0.1',4730);
		}
		return $this->_worker;
	}
	
	//acquisition the work,waiting,circling
	public function workAcquisition(){
		$this->worker()->addFunction('autobid',array($this,'autoBid'));
		while($this->worker()->work());
	}
	
	//do autobid work,per auction
	function autoBid($job){
		$auction=$job->workload();
		$auction=json_decode($auction,true);
		$this->auctionId=$auction['auctionId'];
		$this->customers=$auction['customers'];
        if ($this->validate($this->auctionId)){
    		$this->_log->_logEnd=true;
    		$this->_log->addDir('autobid');
    		$this->_log->addDir(date("Y-m-d"));
    		$this->_log->setFile(date('Y-m-d-H').'_worker'.$this->id.'_'.$this->auctionId);
    		$this->_log->addLog(json_encode($auction),'WORKSTR');
    		if (!Yii::app()->lock->get('AUTOBID_GEARMAN_LOCK:' . $this->auctionId, time()+5)) {
    			$this->_log->addLog(json_encode($auction),'WORKER'.$this->id);
    			$this->AutobidBase();
    			Yii::app()->lock->del('AUTOBID_GEARMAN_LOCK:'.$this->auctionId);
    		}
    		return false;
        }else{
            return false;
        }
	}
	
	//real do the autobid
	public function AutobidBase(){
		$this->_log->addLog(json_encode($this->customers),'AUTO_BID_CUSTOMERS_AUCTION_'.$this->auctionId);
		$autoBid=new AutoBidDao();
		$this->_log->addLog(1,'AUTO_BID_CUSTOMERS_AUCTION_'.$this->auctionId);
		$bidAuction=new BidAuction();
		$this->_log->addLog(2,'AUTO_BID_CUSTOMERS_AUCTION_'.$this->auctionId);
		$bidAuction->setEnv('autoBid');
		$this->_log->addLog(3,'AUTO_BID_CUSTOMERS_AUCTION_'.$this->auctionId);
		if (!empty($this->customers)) {
		    $this->_log->addLog(4,'AUTO_BID_CUSTOMERS_AUCTION_'.$this->auctionId);
				foreach ($this->customers as $customer){
				    $this->_log->addLog(5,'AUTO_BID_CUSTOMERS_AUCTION_'.$this->auctionId);
					//$autoBid->setPrimaryKey($this->auctionId, $customer);
					$autoBid->customerId=$customer['customer_id'];
					$autoBid->auctionId=$this->auctionId;
					$autoBid->autobidId=$customer['autobid_id'];
					
					$bidAuction->customerId=$customer['customer_id'];
					$bidAuction->auctionId=$this->auctionId;
					$this->_log->addLog(6,'AUTO_BID_CUSTOMERS_AUCTION_'.$this->auctionId);
					$bidResult=$bidAuction->bid();
					$this->_log->addLog(json_encode($bidResult),'AUTO_BID_RESULT');
					if($bidResult){
						$autoBid->subAmount();
						$autoBid->setSort();
						$customerBidInfoCache=new customerCacheAuctionBidInfo();
						$customerBidInfoCache->customerId=$customer['customer_id'];
						$customerBidInfoCache->auctionId=$this->auctionId;
						$customerBidInfoCache->addItem('autobid',$autoBid->getAmount());
						break;
					}else{
						$error=$bidAuction->getErrors();
						$this->_log->addLog(json_encode($error),'AUTO_BID_ERRORS');
						$autoBid->addErrorLog($error);
						if(array_key_exists('is_winner',$error)){
							$this->setSort();
						}else{
							$autoBid->delAutoBid();
						}
					}
				}
		}	
	}
	
}